package com.sztle.admodule.handler;

import android.app.Activity;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.CSJAdError;
import com.bytedance.sdk.openadsdk.CSJSplashAd;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.mediation.manager.MediationAdEcpmInfo;
import com.bytedance.sdk.openadsdk.mediation.manager.MediationBaseManager;
import com.sztle.admodule.GmAdManager;
import com.sztle.admodule.ILoadListener;
import com.sztle.admodule.IShowListener;
import com.sztle.admodule.bean.AdConfigInfo;
import com.sztle.admodule.bean.RequestAdEntity;
import com.sztle.admodule.outstatus.AppStatusProcessor;
import com.sztle.admodule.control.AppInfoManager;
import com.sztle.admodule.control.manager.ControlManager;
import com.sztle.admodule.myutils.AdLogUtils;
import com.sztle.admodule.myutils.UIUtils;

public class SplashHandler {

    private static final String TAG = "SplashHandler";

    private static CSJSplashAd useSplashAd;
    private static boolean isLoading = false;
    private static boolean isLoadEnd = false;
    private static boolean isShowing = false;

    public static boolean isLoadEnd() {
        if(useSplashAd != null){
            return false;
        }
        return isLoadEnd;
    }

    public static boolean isShowing(){
        return isShowing;
    }

    public static void setClearFlag(){
        isShowing = false;
        isLoading = false;
    }

    public static boolean canLoad(){
        if(!GmAdManager.isAdSdkReady()){
            return false;
        }
        if (isLoading) {
            return false;
        }
        if (isShowing) {
            return false;
        }

        return true;
    }
    private static String getCode(int position){
        //0 开屏广告位1 1开屏广告位2
        AdConfigInfo adConfigInfo = ControlManager.getAdConfigInfo();
        if(position == 0){
            if(AppInfoManager.isHomeActivityExist()){
                //热启动
                AdLogUtils.Log(TAG, "热启动开屏1加载:");
                if(adConfigInfo != null && !TextUtils.isEmpty(adConfigInfo.splashHotStart1Id.trim())){
                    return adConfigInfo.splashHotStart1Id;
                }
                return AppStatusProcessor.getAppParam().splashAdId;
            } else {
                AdLogUtils.Log(TAG, "冷启动开屏1加载:");
                if(adConfigInfo != null && !TextUtils.isEmpty(adConfigInfo.splashColdStart1Id.trim())){
                    return adConfigInfo.splashColdStart1Id;
                }
                return AppStatusProcessor.getAppParam().splashAdId;
            }
        } else {
            if(AppInfoManager.isHomeActivityExist()){
                //热启动
                AdLogUtils.Log(TAG, "热启动开屏2加载:");
                if(adConfigInfo != null && !TextUtils.isEmpty(adConfigInfo.splashHotStart2Id.trim())){
                    return adConfigInfo.splashHotStart2Id;
                }
                return AppStatusProcessor.getAppParam().splashAdId;
            } else {
                AdLogUtils.Log(TAG, "冷启动开屏2加载:");
                if(adConfigInfo != null && !TextUtils.isEmpty(adConfigInfo.splashColdStart2Id.trim())){
                    return adConfigInfo.splashColdStart2Id;
                }
                return AppStatusProcessor.getAppParam().splashAdId;
            }
        }

    }

    public static void loadAndShow(int position,Activity activity, ViewGroup adContainer, IShowListener showListener) {
        if (activity == null || adContainer == null) {
            if (showListener != null) {
                showListener.onJustEnd(false);
            }
            return;
        }
        if (!canLoad()) {
            if (showListener != null) {
                showListener.onJustEnd(false);
            }
            return;
        }
        if (isReady()) {
            show(adContainer, showListener);
            return;
        }
        String codeId = getCode(position);
        AdLogUtils.Log(TAG,"加载开屏ID："+codeId);
        GmAdManager.enterRequestAdQueue(new RequestAdEntity(RequestAdEntity.TYPE_AD_SPLASH));
        /** 1、创建AdSlot对象 */
        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(codeId)
                .setImageAcceptedSize(UIUtils.getScreenWidthInPx(activity), UIUtils.getScreenHeightInPx(activity))
                .build();

        /** 2、创建TTAdNative对象 */
        TTAdNative adNativeLoader = GmAdManager.get().createAdNative(activity);
        isLoading = true;
        isLoadEnd = false;
        isShowing = false;
        useSplashAd = null;
        adNativeLoader.loadSplashAd(adSlot, new TTAdNative.CSJSplashAdListener() {
            @Override
            public void onSplashLoadSuccess(CSJSplashAd csjSplashAd) {
                useSplashAd = csjSplashAd;
                isLoading = false;
                isLoadEnd = true;

                if (activity != null && !activity.isDestroyed()) {
                    show(adContainer, showListener);
                }
                if (showListener != null) {
                    showListener.onAdLoadEnd(true);
                }
                AdLogUtils.Log(TAG, "onSplashLoadSuccess:");
            }

            @Override
            public void onSplashLoadFail(CSJAdError csjAdError) {
                isLoading = false;
                isLoadEnd = true;
                if (showListener != null) {
                    showListener.onAdLoadEnd(false);
                }
                AdLogUtils.Log(TAG, "onSplashLoadFail:" + csjAdError.getCode() + ":" + csjAdError.getMsg());
            }

            @Override
            public void onSplashRenderSuccess(CSJSplashAd csjSplashAd) {

            }


            @Override
            public void onSplashRenderFail(CSJSplashAd csjSplashAd, CSJAdError csjAdError) {
                AdLogUtils.Log(TAG, "onSplashRenderFail:" + csjAdError.getCode() + ":" + csjAdError.getMsg());
            }
        }, 3500);
    }

    private static ILoadListener loadListener;
    public static void preLoad(int position,Activity activity,ILoadListener listener) {
        if (activity == null) {
            return;
        }
        loadListener = listener;
        if(!canLoad()){
            if (loadListener != null) {
                loadListener.onLoadFail();
            }
            return;
        }
        if (isReady()) {
            if(loadListener != null){
                loadListener.onLoadSuccess();
            }
            return;
        }
        AdLogUtils.Log(TAG, "preLoad start");
        GmAdManager.enterRequestAdQueue(new RequestAdEntity(RequestAdEntity.TYPE_AD_SPLASH));
        /** 1、创建AdSlot对象 */
        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(getCode(position))
                .setImageAcceptedSize(UIUtils.getScreenWidthInPx(activity), UIUtils.getScreenHeightInPx(activity))
                .build();

        /** 2、创建TTAdNative对象 */
        TTAdNative adNativeLoader = GmAdManager.get().createAdNative(activity);
        isLoading = true;
        isLoadEnd = false;
        isShowing = false;
        useSplashAd = null;
        adNativeLoader.loadSplashAd(adSlot, new TTAdNative.CSJSplashAdListener() {
            @Override
            public void onSplashLoadSuccess(CSJSplashAd csjSplashAd) {
                useSplashAd = csjSplashAd;
                isLoading = false;
                isLoadEnd = true;

                if(loadListener != null){
                    loadListener.onLoadSuccess();
                }
                AdLogUtils.Log(TAG, "onSplashLoadSuccess:");
            }

            @Override
            public void onSplashLoadFail(CSJAdError csjAdError) {
                isLoading = false;
                isLoadEnd = true;
                if(loadListener != null){
                    loadListener.onLoadFail();
                }
                AdLogUtils.Log(TAG, "onSplashLoadFail:" + csjAdError.getCode() + ":" + csjAdError.getMsg());
            }

            @Override
            public void onSplashRenderSuccess(CSJSplashAd csjSplashAd) {

            }

            @Override
            public void onSplashRenderFail(CSJSplashAd csjSplashAd, CSJAdError csjAdError) {
                AdLogUtils.Log(TAG, "onSplashRenderFail:" + csjAdError.getCode() + ":" + csjAdError.getMsg());
            }
        }, 3500);
    }

    public static boolean isReady() {
        if (useSplashAd == null) {
            return false;
        }
        if (useSplashAd.getMediationManager() == null) {
            return false;
        }

        return useSplashAd.getMediationManager().isReady();
    }

    public static void show(ViewGroup adContainer, IShowListener showListener) {
//        if (!AdManager.isUseAdExp()) {
//            if (showListener != null) {
//                showListener.onJustEnd(false);
//            }
//            return;
//        }
        if (!isReady()) {
            if (showListener != null) {
                showListener.onJustEnd(false);
            }
            return;
        }
        isShowing = true;
        useSplashAd.setSplashAdListener(new CSJSplashAd.SplashAdListener() {
            @Override
            public void onSplashAdShow(CSJSplashAd csjSplashAd) {
                if (showListener != null) {
                    showListener.onAdShow(getEcpm());
                }
                isShowing = true;
                AdLogUtils.Log(TAG, "onSplashAdShow:");
            }

            @Override
            public void onSplashAdClick(CSJSplashAd csjSplashAd) {

            }

            @Override
            public void onSplashAdClose(CSJSplashAd csjSplashAd, int i) {
                isShowing = false;
                adContainer.setVisibility(View.GONE);
                if (showListener != null) {
                    showListener.onJustEnd(true);
                }
                if (useSplashAd != null) {
                    useSplashAd.getMediationManager().destroy();
                    useSplashAd = null;
                }

            }

        });
        if (adContainer.getVisibility() != View.VISIBLE) {
            adContainer.setVisibility(View.VISIBLE);
        }
        View splashView = useSplashAd.getSplashView();
        UIUtils.removeFromParent(splashView);
        adContainer.removeAllViews();
        adContainer.addView(splashView);
    }

    private static String getEcpm() {
        if (useSplashAd == null) {
            return "";
        }
        MediationBaseManager mediationManager = useSplashAd.getMediationManager();
        if (mediationManager != null) {
            MediationAdEcpmInfo showEcpm = mediationManager.getShowEcpm();
            return showEcpm.getEcpm();
        }
        return "";
    }
}
